package ctrl_cali

import (
	"fmt"
	"iot-base/common/notify"
	"iot-base/common/types"
	"time"
)

type ActionFunc func([]byte) ([]byte, error)

var route = map[string]ActionFunc{
	"ZeroCalibrate":         ZeroCaliAction,
	"SpanCalibrate":         SpanCaliAction,
	"CaliTask":              CaliTasktAction,
	"GetZeroOrSpanStatus":   GetZeroOrSpanStatus,
	"ResetZeroOrSpanStatus": ResetZeroOrSpanStatus,
	"StopCaliTask":          StopCaliTask,
	"QueryRunTask":          QueryRunTask,
}

type Service struct {
}

func NewService() *Service {
	return &Service{}
}

// SubService 订阅服务.
func (s *Service) SubService() {
	notify.EdgeSubActionCmd(func(params *types.ReqActionData) ([]byte, error) {
		if fun, ok := route[params.Action]; ok {
			t := time.Now()
			b, err := fun(params.Params)
			endtime := time.Since(t)
			fmt.Println(params.Action, ":", endtime)
			return b, err
		}
		return nil, notify.ErrActionNotFound
	})
}
